/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis.gui.methoddlg;

import dataanalysis.main.MainView;
import java.awt.Frame;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JOptionPane;
import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ASSearch;
import weka.attributeSelection.AttributeEvaluator;
import weka.attributeSelection.Ranker;
import weka.gui.explorer.ExplorerDefaults;

/**
 *
 * @author Administrator
 */
public final class SchMethodDlg extends BaseMethodDlg implements CommInterface {

    private boolean m_bChanged;

    public SchMethodDlg(Frame parent, boolean modal, MainView manager) {
        super(parent, modal, manager);
    }

    @Override
    public void setEditorValueDoc() {
        m_manager.setASSearchValueDoc((ASSearch) m_backup);
    }

    @Override
    protected void editorChangeAction() {
        m_bChanged = false;
        final ASEvaluation evlValue = m_manager.getEvlValue();
        if (m_objEditor.getValue() instanceof Ranker) {
            if (!(evlValue instanceof AttributeEvaluator)) {
                Object backup = m_objEditor.getBackup();
                int result =
                        JOptionPane.showConfirmDialog(null, "You must use use an evaluator that evaluates\n"
                        + "single attributes (such as InfoGain) in order to use\n"
                        + "the Ranker. Should I select the InfoGain evaluator "
                        + "for you?\n"
                        + "(You can always switch to a different method afterwards)",
                        "Alert!", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    m_manager.setEvlValue(new weka.attributeSelection.InfoGainAttributeEval());
                    m_bChanged = true;
                } else {
                    // restore to what was there previously (if possible)
                    if (backup != null) {
                        m_objEditor.setValue(backup);
                        this.updateTreePanel();
                    }
                }
            }
        } else {
            if (evlValue instanceof AttributeEvaluator) {
                Object backup = m_objEditor.getBackup();
                int result =
                        JOptionPane.showConfirmDialog(null, "You must use use an evaluator that evaluates\n"
                        + "subsets of attributes (such as CFS) in order to use\n"
                        + evlValue.getClass().getName()
                        + ".\nShould I select the CFS subset evaluator for you?"
                        + "\n(you can always switch to a different method afterwards)",
                        "Alert!", JOptionPane.YES_NO_OPTION);

                if (result == JOptionPane.YES_OPTION) {
                    m_manager.setEvlValue(new weka.attributeSelection.CfsSubsetEval());
                    m_bChanged = true;
                } else {
                    // restore to what was there previously (if possible)
                    if (backup != null) {
                        m_objEditor.setValue(backup);
                        this.updateTreePanel();
                    }
                }
            }
        }

        updateUIState();
    }

    @Override
    protected void initObjEditor() {
        Class type = ASSearch.class;
        Object initial = ExplorerDefaults.getASSearch();
        m_objEditor = new MyObjectEditor(type, initial);
        m_manager.setASSearchValueDoc((ASSearch) initial);
        m_objEditor.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent e) {
                editorChangeAction();
            }
        });
    }

    @Override
    protected void OKAction() {
        super.OKAction();
        if (m_bChanged) {
            m_manager.setEvlOK();
        }
    }

    @Override
    protected void cancleAction() {
        super.cancleAction();
        if (m_bChanged) {
            m_manager.setEvlCancle();
        }
    }

    public void setOK() {
        m_backup = m_objEditor.getValue();
        this.setEditorValueDoc();
    }

    public void setCancle() {
        this.recover();
    }

    public Object getEditorValue() {
        return m_objEditor.getValue();
    }

    public void setEditorValue(Object obj) {
        m_objEditor.setValue(obj);
        this.updateTreePanel();
    }
}
